# Contributor: Fabricio Silva <hi@fabricio.dev>
# Maintainer: Fabricio Silva <hi@fabricio.dev>
pkgname=porla
pkgver=0.40.0
pkgrel=0
pkgdesc="A high performance BitTorrent client for servers and seedboxes."
url="https://porla.org/"
arch="all !riscv64" # riscv64: nodejs keeps failing
license="MIT"
_lua=5.4
_boost=1.84
makedepends="
	linux-headers
	cmake
	samurai
	libtorrent-rasterbar-dev
	boost$_boost-dev
	openssl-dev
	openssl-libs-static
	libsodium-dev
	sqlite-dev
	zlib-dev
	libgit2-dev
	lua$_lua-dev
	nettle-dev
	mbedtls-dev
	nlohmann-json
	gnutls-dev
	npm
	zip
	"
install="$pkgname.pre-install"
subpackages="$pkgname-doc $pkgname-openrc"
options="!check net" # no tests / net for npm
_uwebsockets=20.44.0
_usockets=0.8.6
source="https://github.com/porla/porla/archive/v$pkgver/porla-$pkgver.tar.gz
	https://github.com/uNetworking/uWebSockets/archive/v$_uwebsockets/uwebsockets-$_uwebsockets.tar.gz
	https://github.com/uNetworking/uSockets/archive/v$_usockets/usockets-$_usockets.tar.gz
	0001-lua.cmake.libraries.patch
	porla.initd
	porla.confd
	"

prepare() {
	default_prepare

	# link uSockets to uWebSockets
	rm -rf ../uWebSockets-$_uwebsockets/uSockets
	ln -sfv ../uSockets-$_usockets ../uWebSockets-$_uwebsockets/uSockets
	# link uWebSockets to vendor
	rm -rf vendor/uWebSockets
	ln -sfv ../../uWebSockets-$_uwebsockets vendor/uWebSockets

	npm --prefix html ci
}

build() {
	npm --prefix html run build
	(
		cd html/dist
		zip -9 -r ../webui.zip ./*
	)

	export GITVERSION_SEMVER="$pkgver-r$pkgrel.alpine"
	cmake -S . -B build -G Ninja -Wno-dev \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DLUA_INCLUDE_DIR="$(pkg-config --variable=includedir lua$_lua)" \
		-DLUA_LIBRARIES="$(pkg-config --variable=libdir lua$_lua)/liblua.so"
	cmake --build build
}

package() {
	install -Dm755 build/$pkgname "$pkgdir"/usr/bin/$pkgname
	install -Dm644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

sha512sums="
262e53662499901cb04e77f09a37251ead3725f3b21f8999c207276c6249b9c053c575541a16eabb5e899d783e6c4e085473ae219d2712e5f5b24596f58c6108  porla-0.40.0.tar.gz
ceb950a3d4c2ffd58867ef1cc92d5405441dff603ceb6958ef5660f93c8e1da2346cecb43d209ab08d4feac9c1f023f7bff22a13f537dbb3eab0f122f27f70f6  uwebsockets-20.44.0.tar.gz
a4f71e146003056c4a3e99512e6989fd4a37b5943dfcaf593b7b276231c426abd2e1f60de752443fb62ac5926ecf9812866af3c8ce6c8eeeddffc92fa0eb5afb  usockets-0.8.6.tar.gz
6ead5dcf414a01c2db8089890ab44ace53b3ade08dce0a317d00459f0163382e3966e922dddadb0531810859fe7973b096e98ee4eaff44b7f2da5dc2fe46129d  0001-lua.cmake.libraries.patch
27e9321fb186bc6239a27dcf57a2821828f188f0563181aa3aa02daa4d6ffaa68aa8821a127f13444600f5826989d7650e56b12d425dd1dd64c30ecae11bc8ba  porla.initd
24a89c8abdc0f63ccfc90644abb55fe85d8bbf559bbbfc139daade60d29ca77b7e594b7e633a9e4dec68ebc094175e1ffa9e17eccb05c5e9f10ea4ba0dd4fd3d  porla.confd
"
